<?php

class HTree
{
    private $data = array();
    private $object;
    private $results = array();
    
    public function __construct( array $data = array() )
    {
        $this->data = $data;
        $this->getObjectInstance();
    }
    
    public function export( $initial = 0, $level = 0 )
    {
        $this->results = array();
        $this->search( $initial, $level );            
        return $this->results;
    }
    
    private function search( $seek, $level )
    {
        $filter = $this->getFilterInstance( $seek );
        foreach( $filter as $result )
        {
            $result[ 'level' ] = $level;
            $this->results[] = $result;
            $this->search( $result['id'], ( $level + 1 ) );
        }
    }
    
    private function getFilterInstance( $seek )
    {
        return new HIterator( $this->object->getIterator(), $seek );
    }
    
    private function getObjectInstance()
    {
        if( ! empty( $this->object ) ) return $this->object;            
        return ( $this->object = new ArrayObject( $this->data ) );
    }
}
